'use strict';
const dayjs = require('dayjs');
module.exports = app => {
    const { INTEGER, STRING } = app.Sequelize;
    const CourseModules = app.model.define(
        'CourseModules',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            course_id: INTEGER,
            name: STRING,
            description: STRING,
            sort: INTEGER,
            status: INTEGER,
            delete_flag: INTEGER,
            created_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('created_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
                type: app.Sequelize.DATE,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'course_modules',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'created_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    CourseModules.associate = function() {
        app.model.CourseModules.belongsTo(app.model.Course, {foreignKey: 'course_id', targetKey: 'id'});
        app.model.CourseModules.hasMany(app.model.CourseItem, {foreignKey: 'course_module_id', sourceKey: 'id'});
    }
    return CourseModules;
};
